package com.eightgroups.tces.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.eightgroups.common.result.JsonResult;
import com.eightgroups.common.utils.StringUtil;
import com.eightgroups.tces.entity.Course;
import com.eightgroups.tces.service.ICourseService;
import com.eightgroups.tces.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;
import com.eightgroups.common.controller.BaseController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author zzc2016.cn
 * @since 2017-11-25
 */
@Controller
@RequestMapping("/admin/course")
public class CourseController extends BaseController {
    @Autowired
    private ICourseService iCourseService;

    /**
     * 系部列表页面
     * @return
     */
    @GetMapping("/list")
    public String list(){
        return "admin/course/list";
    }
    /**
     * 获取课程列表
     * @param pageNumber 当前页
     * @param pageSize 每页显示条数
     * @param searchText 搜索名称
     * @return
     */
    @RequestMapping("/getList")
    @ResponseBody
    public Map<String,Object> getList(int pageNumber, int pageSize, String searchText){
        Map<String,Object> result = new HashMap<String,Object>();
        Page<Course> coursePage = new Page<>(pageNumber,pageSize);
        EntityWrapper<Course> entityWrapper = new EntityWrapper<>();
        if(StringUtil.isNotEmpty(searchText)){
            entityWrapper.like("cname",searchText);
        }
        Page<Course> page = iCourseService.selectPage(coursePage, entityWrapper);
        result.put("total", page.getTotal());
        result.put("rows", page.getRecords());
        return result;
    }
    /**
     * 添加课程
     * @param course
     * @return
     */
    @RequestMapping("/addCourse")
    @ResponseBody
    public JsonResult addCourse(Course course){
        return iCourseService.insert(course)?renderSuccess("添加成功"):renderError("添加失败");
    }
    @RequestMapping("/updateCourseView/{id}")
    public String updateCoursewareView(@PathVariable("id") String id, Model model) {
        EntityWrapper<Course> wrapper = new EntityWrapper<>();
        wrapper.eq("id", id);
        Course course = iCourseService.selectOne(wrapper);
        model.addAttribute("course", course);
        return "admin/course/update";
    }
    /**
     * 修改课程
     * @param course
     * @return
     */
    @RequestMapping("/updateCourse")
    @ResponseBody
    public JsonResult updateDept(Course course){
        return iCourseService.updateById(course)?renderSuccess("修改成功"):renderError("修改失败");
    }
    /**
     * 删除课程
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("/delete")
    public JsonResult delete(@RequestParam(value = "id", required = false) String id) {
        if (StringUtil.isEmpty(id)) {
            return renderError("请选择数据");
        }
        String[] split = id.split(",");
        List<String> ids = new ArrayList<>();
        for (String item:split) {
            ids.add(item);
        }
        return iCourseService.deleteBatchIds(ids)? renderSuccess("删除成功") : renderError("删除失败");
    }
}
